package com.example.farmer.Adapter;

import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.farmer.Entity.CartItem;
import com.example.farmer.R;
import com.example.farmer.Database.CartDatabase;

import java.util.List;

public class CartAdapter extends BaseAdapter {
    private Context context;
    private List<CartItem> cartItems;
    private SparseBooleanArray selectedItems;
    private CartDatabase cartDatabase; // 数据库操作类
    private boolean allSelected = false;

    public CartAdapter(Context context, List<CartItem> cartItems) {
        this.context = context;
        this.cartItems = cartItems;
        selectedItems = new SparseBooleanArray();
        this.cartDatabase = new CartDatabase(context); // 初始化数据库操作类
    }

    @Override
    public int getCount() {
        return cartItems.size();
    }

    @Override
    public Object getItem(int position) {
        return cartItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.cart_item, parent, false);
        }

        CheckBox checkbox = convertView.findViewById(R.id.checkbox);
        ImageView image = convertView.findViewById(R.id.product_image);
        TextView name = convertView.findViewById(R.id.product_name);
        TextView price = convertView.findViewById(R.id.product_price);
        TextView quantity = convertView.findViewById(R.id.product_quantity);
        ImageButton btnDecreaseQuantity = convertView.findViewById(R.id.btn_decrease_quantity);
        ImageButton btnIncreaseQuantity = convertView.findViewById(R.id.btn_increase_quantity);
        TextView remove = convertView.findViewById(R.id.remove);

        // 绑定数据
        final CartItem item = cartItems.get(position);

        // 假设您已经配置了Glide用于图片加载
        Glide.with(context)
                .load(item.getProductImage())
                .into(image);
        name.setText(item.getProductName());
        price.setText(String.format("¥%.2f", item.getPrice()));
        quantity.setText(String.valueOf(item.getQuantity()));

        // 设置复选框状态
        checkbox.setChecked(selectedItems.get(position, false));
        checkbox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            selectedItems.put(position, isChecked);
        });

        // 为删除按钮设置点击事件
        remove.setOnClickListener(v -> {
            // 从列表和数据库中删除商品，并刷新ListView
            cartItems.remove(item);
            cartDatabase.deleteCartItem(item.getId()); // 调用数据库删除方法
            notifyDataSetChanged();
            Toast.makeText(context, "商品已移除", Toast.LENGTH_SHORT).show();
        });

        // 为数量增减按钮设置点击事件
        btnDecreaseQuantity.setOnClickListener(v -> {
            if (item.getQuantity() > 1) {
                item.setQuantity(item.getQuantity() - 1);
                cartDatabase.updateCartItem(item); // 更新数据库
                quantity.setText(String.valueOf(item.getQuantity()));
                notifyDataSetChanged();
            }
        });

        btnIncreaseQuantity.setOnClickListener(v -> {
            item.setQuantity(item.getQuantity() + 1);
            cartDatabase.updateCartItem(item); // 更新数据库
            quantity.setText(String.valueOf(item.getQuantity()));
            notifyDataSetChanged();
        });

        return convertView;
    }

    public void setAllSelected(boolean isSelected) {
        this.allSelected = isSelected;
        if (isSelected) {
            for (int i = 0; i < cartItems.size(); i++) {
                selectedItems.put(i, true);
            }
        } else {
            selectedItems.clear();
        }
        notifyDataSetChanged();
    }

    public boolean isSelected(int position) {
        return allSelected || selectedItems.get(position, false);
    }

    // 可以添加其他需要的方法，比如根据选中状态更新数据库等
}